/*
 * @(#)CirculateAglet.java
 *
 * 03L7246 (c) Copyright IBM Corp. 1996, 1998
 *
 * The program is provided "as is" without any warranty express or
 * implied, including the warranty of non-infringement and the implied
 * warranties of merchantibility and fitness for a particular purpose.
 * IBM will not be liable for any damages suffered by you as a result
 * of using the Program. In no event will IBM be liable for any
 * special, indirect or consequential damages or lost profits even if
 * IBM has been advised of the possibility of their occurrence. IBM
 * will not be liable for any third party claims against you.
 */

package example.itinerary;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Checkbox;
import java.awt.Choice;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.List;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.Enumeration;
import java.util.Vector;

import com.ibm.aglet.Aglet;
import com.ibm.aglet.AgletProxy;
import com.ibm.aglet.InvalidAgletException;
import com.ibm.aglet.Message;
import com.ibm.aglet.util.AddressChooser;
import com.ibm.agletx.util.SeqPlanItinerary;
/**
 * <tt> CirculateAglet </tt> illustrates how to use SeqPlanItinerary.
 *
 * @version     1.00	$Date: 1999/10/27 05:16:39 $
 * @author	Mitsuru Oshima
 */
public class CirculateAglet extends Aglet {
    StringBuffer buffer;
    SeqPlanItinerary itinerary;
    Vector proxies;

    public void onCreation(Object ini) {
		itinerary = new SeqPlanItinerary(this);
		itinerary.addPlan("atp://sirius.trl.ibm.com:2000/", "getLocalInfo");
		itinerary.addPlan("atp://vmoshima.trl.ibm.com/", "getProxies");
		itinerary.addPlan(getAgletContext().getHostingURL().toString(),
			 	 "printResult");
    }

    public boolean handleMessage(Message msg) {
		if (msg.sameKind("getLocalInfo")) {
	    	getLocalInfo(msg);
	    	return true;
		} else if (msg.sameKind("getProxies")) {
	    	getProxies(msg);
	    	return true;
		} else if (msg.sameKind("dialog")) {
	    	CirculateFrame f = new CirculateFrame(this);
	    	f.pack();
	    	f.setVisible(true);
	    	init();
	    	return true;
		} else if (msg.sameKind("printResult")) {
	    	System.out.println( buffer );
	    	Enumeration e = proxies.elements();
	    	while(e.hasMoreElements()) {
				AgletProxy p = (AgletProxy)e.nextElement();
				try {
		    		System.out.println(p.getAgletInfo());
				} catch (InvalidAgletException ex) {
		    		System.out.println("InvalidAglet");
				}
	    	}
	    	return true;
		}
		return false;
    }

    private void init() {
		buffer = new StringBuffer();
		proxies = new Vector();
    }

    public void oncemore() {
		try {
	    	itinerary.startTrip();
		} catch (Exception ex) {
	    	ex.printStackTrace();
		}
    }

    public void start() {
		init();
		oncemore();
    }

    public void getLocalInfo(Message msg) {
		buffer.append("Username : "+getProperty("user.name"));
		buffer.append("\n");
		buffer.append("Home directory : "+getProperty("user.home"));
		buffer.append("\n");
		buffer.append("Currect working directory : "+getProperty("user.dir"));
		buffer.append("\n");
		buffer.append("Machine architecture : "+getProperty("os.arch"));
		buffer.append("\n");
		buffer.append("OS name : "+getProperty("os.name"));
		buffer.append("\n");
		buffer.append("OS version : "+getProperty("os.version"));
		buffer.append("\n");
		buffer.append("Java version : "+getProperty("java.version"));
		buffer.append("\n");
    }

    private String getProperty(String key) {
		return System.getProperty(key, "Unknown");
    }

    public void getProxies(Message msg) {
		Enumeration e = getAgletContext().getAgletProxies(ACTIVE);

		while(e.hasMoreElements()) {
	    	proxies.addElement(e.nextElement());
		}
    }
}

class CirculateFrame extends Frame implements WindowListener, ActionListener ,ItemListener{
    CirculateAglet aglet;
    List list = new List(10, false);
    AddressChooser address = new AddressChooser(15);
    Choice choice = new Choice();
    Checkbox check = new Checkbox("Repeat");
    
    CirculateFrame(CirculateAglet a) {
		aglet = a;
		setLayout(new BorderLayout());
		add("Center", list);
		addWindowListener(this);
		check.addItemListener(this);
		
		Panel p = new Panel();
		p.setLayout(new FlowLayout());
		p.add(address);
		p.add(choice);
		
		Button ad = new Button("Add");
		Button remove = new Button("Remove");
		ad.addActionListener(this);
		remove.addActionListener(this);
		p.add(ad);
		p.add(remove);
		add("North", p);

		p = new Panel();
		p.setLayout(new FlowLayout());
		p.add(check);
		
		Button once = new Button("Once More");
		Button start = new Button("Start!");
		once.addActionListener(this);
		start.addActionListener(this);
		p.add(once);
		p.add(start);
		add("South", p);

		
		choice.addItem("getLocalInfo");
		choice.addItem("getProxies");
		choice.addItem("printResult");

		update();
    }
    /**
     * Handles the action event
     * @param ae the event to be handled
     */
	public void actionPerformed(ActionEvent ae){
		if("Once More".equals(ae.getActionCommand())){
			aglet.oncemore();
	    }else if("Start!".equals(ae.getActionCommand())){
			aglet.start();
		}else if("Remove".equals(ae.getActionCommand())){
			int i = list.getSelectedIndex();
			if (i>=0) {
		    	aglet.itinerary.removePlanAt(i);
		    	list.delItem(i);
			}
		}else if("Add".equals(ae.getActionCommand())){
			aglet.itinerary.addPlan(address.getAddress(),choice.getSelectedItem());
			update();
		}
	}
	
	public void itemStateChanged(ItemEvent ie){
		aglet.itinerary.setRepeat(check.getState());
	}
	
	/**
     * Handles the window event
     * @param we the event to be handled
     */
	
	public void windowClosing(WindowEvent we){
		dispose();
	}
	
	public void windowOpened(WindowEvent we){
	}
	
	public void windowIconified(WindowEvent we){
	}
	
	public void windowDeiconified(WindowEvent we){
	}
	
	public void windowClosed(WindowEvent we){
	}
	
	public void windowActivated(WindowEvent we){
	}
	
	public void windowDeactivated(WindowEvent we){
	}
/*
    public boolean handleEvent(java.awt.Event ev) {
		if (ev.id == java.awt.Event.WINDOW_DESTROY) {
	    	dispose();
	    	return true;
		}
		return super.handleEvent(ev);
    }

    public boolean action(java.awt.Event ev, Object obj) {
		if (ev.target instanceof java.awt.Button) {
	    	Button b = (Button)ev.target;
	    String l = b.getLabel();
	    if ("Once More".equals(l)) {
		aglet.oncemore();
	    } else if ("Start!".equals(l)) {
		aglet.start();
	    } else if ("Remove".equals(l)) {
		int i = list.getSelectedIndex();
		if (i>=0) {
		    aglet.itinerary.removePlanAt(i);
		    list.delItem(i);
		}
	    } else if ("Add".equals(l)){
		aglet.itinerary.addPlan(address.getAddress(),
					choice.getSelectedItem());
		update();
	    }
	    return true;
	} else if (ev.target instanceof java.awt.Checkbox) {
	    aglet.itinerary.setRepeat(check.getState());
	}
	return false;
    }
*/
    private void update() {
	list.removeAll();
	SeqPlanItinerary spi = aglet.itinerary;
	int size = spi.size();
	for(int i = 0; i< size; i++) {
	    String s = spi.getAddressAt(i) + " : " + spi.getMessageAt(i).getKind();
	    list.addItem(s);
	}
	check.setState(aglet.itinerary.isRepeat());
    }
}
